//
//  LZSearchTrackTableViewCell.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/13.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "LZSearchTrackTableViewCell.h"
#import "FitHeader.h"
#import "Model.h"
#import "UIImageView+WebCache.h"
@implementation LZSearchTrackTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
        [self changeColor];
    }
    return self;
}

-(void)changeColor{
    self.backgroundColor = [Model shareInstance].ViewColor;
}

- (void)createView{
    _coverPathImageView = [[UIImageView alloc]init];
    [self.contentView addSubview:_coverPathImageView];
    
    _titleTopLabel = [[BaseLabel alloc]init];
    [_titleTopLabel setFontSize:16];
    _titleTopLabel.numberOfLines = 0;
    [self.contentView addSubview:_titleTopLabel];
    
    _titleTopSubLabel = [[BaseLabel alloc]init];
    [_titleTopSubLabel setFontSize:14];
    [self.contentView addSubview:_titleTopSubLabel];
    
    _titleCenterLabel = [[BaseLabel alloc]init];
    [_titleCenterLabel setFontSize:14];
    _titleCenterLabel.textColor = [UIColor lightGrayColor];
    [self.contentView addSubview:_titleCenterLabel];
    
    _playImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:_playImageView];
    
    _loveImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:_loveImageView];
    
    _remarkImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:_remarkImageView];
    
    _timeImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:_timeImageView];
    
    _titleBottomLeftLabel = [[BaseLabel alloc]init];
    [_titleBottomLeftLabel setFontSize:13];
    _titleBottomLeftLabel.textColor = [UIColor lightGrayColor];
    [self.contentView addSubview:_titleBottomLeftLabel];
    
    _titleBottomMidLeftLabel = [[BaseLabel alloc]init];
    [_titleBottomMidLeftLabel setFontSize:13];
    _titleBottomMidLeftLabel.textColor = [UIColor lightGrayColor];
    [self.contentView addSubview:_titleBottomMidLeftLabel];
    
    _titleBottomMidRightLabel = [[BaseLabel alloc]init];
    [_titleBottomMidRightLabel setFontSize:13];
    _titleBottomMidRightLabel.textColor = [UIColor lightGrayColor];
    [self.contentView addSubview:_titleBottomMidRightLabel];
    
    _titleBottomRightLabel = [[BaseLabel alloc]init];
    [_titleBottomRightLabel setFontSize:13];
    _titleBottomRightLabel.textColor = [UIColor lightGrayColor];
    [self.contentView addSubview:_titleBottomRightLabel];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    _coverPathImageView.frame = CGRectMake(10 * FITWidth, 10 * FITHeight, 80 * FITWidth, 80 * FITHeight);
    _coverPathImageView.layer.masksToBounds = YES;
    _coverPathImageView.layer.cornerRadius = 40 * FITHeight;
    
    _titleTopLabel.frame = CGRectMake(_coverPathImageView.frame.origin.x + _coverPathImageView.frame.size.width + 10 * FITWidth, _coverPathImageView.frame.origin.y, 230 * FITWidth, 30 * FITHeight);
    
    _titleTopSubLabel.frame = CGRectMake(_titleTopLabel.frame.origin.x + _titleTopLabel.frame.size.width, 15 * FITHeight, 55 * FITWidth, 20 * FITHeight);
    [_titleTopSubLabel setFontSize:12];
    _titleTopSubLabel.textAlignment = 1;
    _titleTopSubLabel.textColor = [UIColor lightGrayColor];
    
    _titleCenterLabel.frame = CGRectMake(_titleTopLabel.frame.origin.x, _titleTopLabel.frame.origin.y + _titleTopLabel.frame.size.height, 200 * FITWidth, 25 * FITHeight);
    
    _playImageView.frame = CGRectMake(_titleCenterLabel.frame.origin.x, _titleCenterLabel.frame.origin.y + _titleCenterLabel.frame.size.height + 4 * FITHeight, 8 * FITHeight, 8 * FITHeight);
    
    _titleBottomLeftLabel.frame = CGRectMake(_titleCenterLabel.frame.origin.x + 8 * FITWidth, _titleCenterLabel.frame.origin.y + _titleCenterLabel.frame.size.height, 40 * FITWidth, 15 * FITHeight);
    
    _loveImageView.frame = CGRectMake(_titleBottomLeftLabel.frame.origin.x + _titleBottomLeftLabel.frame.size.width, _playImageView.frame.origin.y, 8 * FITHeight, 8 * FITHeight);
    
    _titleBottomMidLeftLabel.frame = CGRectMake(_loveImageView.frame.origin.x + 8 * FITWidth, _titleBottomLeftLabel.frame.origin.y, 35 * FITWidth, _titleBottomLeftLabel.frame.size.height);
    
    _remarkImageView.frame =CGRectMake(_titleBottomMidLeftLabel.frame.origin.x + _titleBottomMidLeftLabel.frame.size.width, _playImageView.frame.origin.y, 8 * FITHeight, 8 * FITHeight);
    
    _titleBottomMidRightLabel.frame = CGRectMake(_remarkImageView.frame.origin.x + 8 * FITWidth, _titleBottomLeftLabel.frame.origin.y, _titleBottomMidLeftLabel.frame.size.width + 20 * FITWidth, _titleBottomLeftLabel.frame.size.height);
    
    _timeImageView.frame = CGRectMake(_titleBottomMidRightLabel.frame.origin.x + _titleBottomMidLeftLabel.frame.size.width + 20 * FITWidth, _playImageView.frame.origin.y, 8 * FITHeight, 8 * FITHeight);
    
    _titleBottomRightLabel.frame = CGRectMake(_timeImageView.frame.origin.x + 8 * FITWidth, _titleBottomLeftLabel.frame.origin.y, _titleBottomMidLeftLabel.frame.size.width, _titleBottomLeftLabel.frame.size.height);
    
    _playImageView.image = [UIImage imageNamed:@"S1mpl3播放"];
    _loveImageView.image = [UIImage imageNamed:@"S1mpl3收藏"];
    _remarkImageView.image = [UIImage imageNamed:@"S1mpl3评论"];
    _timeImageView.image = [UIImage imageNamed:@"S1mpl3时钟"];
    
}



-(void)setDic:(NSDictionary *)dic{
    if (_dic != dic) {
        _dic = dic;
    }
    if (![[dic objectForKey:@"album_cover_path"] isEqual:[NSNull null]]) {
        
        [_coverPathImageView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"album_cover_path"]] placeholderImage:[UIImage imageNamed:@"100*100"]];
    }
    _titleTopLabel.text = [dic objectForKey:@"title"];
    if ([_titleBottomLeftLabel.text integerValue] < 10000.0) {
        _titleBottomLeftLabel.text = [NSString stringWithFormat:@"%@", [dic objectForKey:@"count_play"]];
    }else{
        _titleBottomLeftLabel.text = [NSString stringWithFormat:@"%.1f", [[dic objectForKey:@"count_play"] integerValue] / 10000.0];
    }
    _titleCenterLabel.text = [NSString stringWithFormat:@"by %@", [dic objectForKey:@"nickname"]];
    _titleBottomMidLeftLabel.text = [NSString stringWithFormat:@"%@", [dic objectForKey:@"count_like"]];
    _titleBottomMidRightLabel.text = [NSString stringWithFormat:@"%@", [dic objectForKey:@"duration"]];
    NSInteger time = [[dic objectForKey:@"duration"] integerValue];
    NSInteger hour = time / 60;
    NSInteger minute = time - hour * 60;
    if (minute < 10) {
        _titleBottomRightLabel.text = [NSString stringWithFormat:@"%ld:0%ld", hour, minute];
    }else

        _titleBottomRightLabel.text = [NSString stringWithFormat:@"%ld:%ld", hour, minute];
    [self changeColor];
}

//-(void)setVideoDic:(NSDictionary *)videoDic{
//    if (_videoDic != videoDic) {
//        _videoDic = videoDic;
//    }
//    [_coverPathImageView sd_setImageWithURL:[NSURL URLWithString:[videoDic objectForKey:@"coverSmall"]] placeholderImage:[UIImage imageNamed:@""]];
//    _titleTopLabel.text = [videoDic objectForKey:@"title"];
//    _titleTopSubLabel.text = [videoDic objectForKey:@""];
//    _titleCenterLabel.text = [NSString stringWithFormat:@"by %@", [videoDic objectForKey:@"nickname"]];
//    
//    if ([_titleBottomLeftLabel.text integerValue] < 10000.0) {
//        _titleBottomLeftLabel.text = [NSString stringWithFormat:@"%@", [videoDic objectForKey:@"playsCounts"]];
//    }else{
//        
//        _titleBottomLeftLabel.text = [NSString stringWithFormat:@"%.1f万", [[videoDic objectForKey:@"playsCounts"] floatValue] / 10000.0];
//    }
//    _titleBottomMidLeftLabel.text = [NSString stringWithFormat:@"%@", [videoDic objectForKey:@"favoritesCounts"]];
//    _titleBottomMidRightLabel.text = [NSString stringWithFormat:@"%@", [videoDic objectForKey:@"commentsCounts"]];
//    NSInteger time = [[videoDic objectForKey:@"duration"] integerValue];
//    NSInteger hour = time / 60;
//    NSInteger minute = time - hour * 60;
//    if (minute < 10) {
//        _titleBottomRightLabel.text = [NSString stringWithFormat:@"%ld:0%ld", hour, minute];
//    }else
//        _titleBottomRightLabel.text = [NSString stringWithFormat:@"%ld:%ld", hour, minute];
//    NSInteger date = [[videoDic objectForKey:@"createdAt"] integerValue] / 1000;
//    NSDate *dateString = [NSDate dateWithTimeIntervalSince1970:date];
//    [self changeColor];
//}

+(NSString *) compareCurrentTime:(NSDate*) compareDate
//
{
    NSTimeInterval  timeInterval = [compareDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    long temp = 0;
    NSString *result;
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
    }
    else if((temp = timeInterval/60) <60){
        result = [NSString stringWithFormat:@"%ld分钟前",temp];
    }
    
    else if((temp = temp/60) <24){
        result = [NSString stringWithFormat:@"%ld小时前",temp];
    }
    
    else if((temp = temp/24) <30){
        result = [NSString stringWithFormat:@"%ld天前",temp];
    }
    
    else if((temp = temp/30) <12){
        result = [NSString stringWithFormat:@"%ld月前",temp];
    }
    else{
        temp = temp/12;
        result = [NSString stringWithFormat:@"%ld年前",temp];
    }
    
    return  result;
}

@end
